package love.bot.forli.group.review.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import love.bot.forli.group.review.bean.Result;
import love.bot.forli.group.review.entity.Code;
import love.bot.forli.group.review.mapper.CodeMapper;
import love.bot.forli.group.review.service.CodeService;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Kontori
 */
@Service
public class CodeServiceImpl extends ServiceImpl<CodeMapper, Code> implements CodeService {

    private CodeMapper codeMapper;

    @Autowired
    public void setCodeMapper(CodeMapper codeMapper) {
        this.codeMapper = codeMapper;
    }

    @Override
    public Result<String> createCode(String qq) {
        if (codeMapper.selectOne(new QueryWrapper<Code>()
                .eq("qq", qq)
                .eq("status", 0)) != null) {
            return Result.fail("指定QQ存在未使用的入群码");
        }
        Code code = new Code();
        code.setQq(qq);
        String codeValue = RandomStringUtils.randomAlphanumeric(10);
        code.setValue(codeValue);
        code.setStatus(0);
        codeMapper.insert(code);
        return Result.success("创建成功", codeValue);
    }
}
